﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Ria.ApplicationServices;
using System.Windows.Ria.Data;
using CARE;
using CARE.Web;
using CARE.Web.Services;
using GraphInformation;

namespace CARE
{
	public partial class Source : UserControl
	{
        private GraphProvider gp;

        private IWizardButtonController wizardButtonController;

        private GenerateSocialNetworkWindow generator;

        public Source(IWizardButtonController sw)
        {
            // Required to initialize variables
            InitializeComponent();
            gp = new GraphProvider();

            wizardButtonController = sw;
        }

		private void Import_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			String graphML;
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Filter = "XML Files (*.xml)|*.xml|GraphML Files (*.graphml)|*.graphml";
            Nullable<Boolean> result = dialog.ShowDialog();
            if (result.Value == true)
            {
                graphML = dialog.File.OpenText().ReadToEnd();

                var load = gp.Load(gp.GetGraphFromGraphMLQuery(graphML));
                load.Completed += new EventHandler(load_Completed);
            }
        }

        void load_Completed(object sender, EventArgs e)
        {
            if (wizardButtonController is SpreadVizualization) 
            {
                SimulationParameters simParameters = SimulationParameters.getInstance();
                simParameters.SocialNetworkGraph = gp.Graphs[0];
            } 
            else 
            {
                InoculationParameters inocParameters = InoculationParameters.getInstance();
                inocParameters.SocialNetworkGraph = gp.Graphs[0];
            }
            
            wizardButtonController.EnableNext();
        }
		
		private void Generate_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            generator = new GenerateSocialNetworkWindow(false);
            generator.wizardButtonController = wizardButtonController;
            generator.Show();
		}
		
		private void Database_Click(object sender, System.Windows.RoutedEventArgs e)
		{
			// TODO: Add event handler implementation here.
            var databasesource = new Views.DatabaseSocialNetworkWindow();
            databasesource.Closed += new EventHandler(databasesource_Closed);
            databasesource.Show();
		}

        void databasesource_Closed(object sender, EventArgs e)
        {
            //TODO: convert net to graph
            throw new NotImplementedException();
        }
	}
}